package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.page.PageData;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.OrderDetailDao;
import io.aircraft.modules.wms.dto.OrderDetailDTO;
import io.aircraft.modules.wms.entity.OrderDetailEntity;
import io.aircraft.modules.wms.service.OrderDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 出库明细
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-11-16
 */
@Service
public class OrderDetailServiceImpl extends CrudServiceImpl<OrderDetailDao, OrderDetailEntity, OrderDetailDTO> implements OrderDetailService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageData<OrderDetailDTO> page(Map<String, Object> params) {
        IPage<OrderDetailEntity> page = baseDao.selectPage(
                getPage(params, "detail_id", true),
                getWrapper(params)
        );
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, OrderDetailDTO.class);
    }

    @Override
    public QueryWrapper<OrderDetailEntity> getWrapper(Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        String contNo = (String) params.get("contNo");
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        String detailSt = (String) params.get("detailSt");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");

        QueryWrapper<OrderDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(orderId), "order_id", orderId);
        wrapper.eq(StringUtils.isNotBlank(detailSt), "detail_st", detailSt);
        wrapper.eq(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.eq(StringUtils.isNotBlank(lotNo), "lot_no", lotNo);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_date", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_date", endDate);
        return wrapper;
    }

    @Override
    public List<OrderDetailEntity> getDetailByOrderId(Long id) {
        return baseDao.getDetailByOrderId(id);
    }

    /**
     * 通过订单主键ID获取其对应的所有明细信息
     *
     * @param orderId 主单id
     * @param all     明细表中所有信息
     * @return
     */
    @Override
    public List<OrderDetailDTO> getDetailByOrderId(Long orderId, List<OrderDetailDTO> all) {
        return all.stream().filter(dto -> orderId.equals(dto.getOrderId())).collect(Collectors.toList());
    }

    @Override
    public List<OrderDetailDTO> getAllDetail() {
        List<OrderDetailEntity> entityList = baseDao.selectList(null);
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        entityList.stream().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return ConvertUtils.sourceToTarget(entityList, OrderDetailDTO.class);
    }

    @Override
    public int batchSave(List<OrderDetailDTO> dtoList) {
        List<OrderDetailEntity> detailList = ConvertUtils.sourceToTarget(dtoList, OrderDetailEntity.class);
        return baseDao.batchSave(detailList);
    }

    @Override
    public void save(OrderDetailDTO dto) {
        OrderDetailEntity entity = ConvertUtils.sourceToTarget(dto, OrderDetailEntity.class);
        insert(entity);
    }
}